﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Newtonsoft.Json;

namespace T.FAS.Runtime.Metadata.Entity.MF
{

    public class Microflow
    {
        [JsonProperty("ID")]
        public string ID { get; set; }
        [JsonProperty("Name")]
        public string Name { get; set; }
        [JsonProperty("Description")]
        public string Description { get; set; }
        /// <summary>
        /// 入参
        /// </summary>
        [JsonProperty("Params")]
        public List<MicroflowParam> Params { get; set; }

        [JsonProperty("DesignData")]
        public DesignData DesignData { get; set; }
        /// <summary>
        /// 依赖的元数据
        /// </summary>
        public List<MicroflowDependency> Dependencies { get; set; }
        public List<MicroflowOutput> Outputs { get; set; }
        public string MetaDataID { get; set; }
        public string MetaDataCode { get; set; }
        public string MetaDataName { get; set; }
        public DateTime CreateTime { get; set; }
        public string Creator { get; set; }
        public DateTime LastModifyTime { get; set; }
        public string LastModifier { get; set; }
    }
    // 工作流容器类
    public class DesignData
    {
        [JsonProperty("cells")]
        public List<ShapeBase> Cells { get; set; }
    }

    // 以下为支持类
    public class EdgeAttributes
    {
        [JsonProperty("line")]
        public LineStyle Line { get; set; }
    }

    public class LineStyle
    {
        [JsonProperty("stroke")]
        public string Stroke { get; set; }

        [JsonProperty("strokeDasharray",NullValueHandling = NullValueHandling.Ignore)]
        public string StrokeDashArray { get; set; }
    }

    public class ConnectionPoint
    {
        [JsonProperty("cell")]
        public string Cell { get; set; }

        [JsonProperty("port")]
        public string Port { get; set; }
    }

    public class Position
    {
        [JsonProperty("x")]
        public int X { get; set; }

        [JsonProperty("y")]
        public int Y { get; set; }
    }

    public class Size
    {
        [JsonProperty("width")]
        public int Width { get; set; }

        [JsonProperty("height")]
        public int Height { get; set; }
    }

    public class Ports
    {
        [JsonProperty("groups")]
        public Dictionary<string, PortGroup> Groups { get; set; }

        [JsonProperty("items")]
        public List<PortItem> Items { get; set; }
    }

    public class PortGroup
    {
        [JsonProperty("label")]
        public Label Label { get; set; }

        [JsonProperty("position")]
        public object Position { get; set; } // 可以是字符串或对象

        [JsonProperty("attrs",NullValueHandling = NullValueHandling.Ignore)]
        public PortAttribute PortAttribute { get; set; }

        [JsonProperty("markup")]
        public List<Markup> Markup { get; set; }
    }

    public class OutCircle
    {
        [JsonProperty("r", NullValueHandling = NullValueHandling.Ignore)]
        public int? Radius { get; set; }

        [JsonProperty("magnet", NullValueHandling = NullValueHandling.Ignore)]
        public bool? Magnet { get; set; }

        // 其他属性根据实际需要添加
        [JsonProperty("fill", NullValueHandling = NullValueHandling.Ignore)]
        public string Fill { get; set; }
        [JsonProperty("stroke", NullValueHandling = NullValueHandling.Ignore)]
        public string Stroke { get; set; }
        [JsonProperty("strokeWidth", NullValueHandling = NullValueHandling.Ignore)]
        public int? StrokeWidth { get; set; }
        [JsonProperty("style", NullValueHandling = NullValueHandling.Ignore)]
        public Dictionary<string, object> Style { get; set; }
    }
    public class Circle
    {
        [JsonProperty("r", NullValueHandling = NullValueHandling.Ignore)]
        public int? Radius { get; set; }

        [JsonProperty("magnet", NullValueHandling = NullValueHandling.Ignore)]
        public bool? Magnet { get; set; }

        // 其他属性根据实际需要添加
        [JsonProperty("fill", NullValueHandling = NullValueHandling.Ignore)]
        public string Fill { get; set; }
        [JsonProperty("stroke", NullValueHandling = NullValueHandling.Ignore)]
        public string Stroke { get; set; }
        [JsonProperty("strokeWidth", NullValueHandling = NullValueHandling.Ignore)]
        public int? StrokeWidth { get; set; }
        [JsonProperty("style", NullValueHandling = NullValueHandling.Ignore)]
        public Dictionary<string, object> Style { get; set; }
    }
    public class TextPlus
    {
        [JsonProperty("fill", NullValueHandling = NullValueHandling.Ignore)]
        public string Fill { get; set; }

        [JsonProperty("fontSize", NullValueHandling = NullValueHandling.Ignore)]
        public int? FontSize { get; set; }

        [JsonProperty("ref", NullValueHandling = NullValueHandling.Ignore)]
        public string Ref { get; set; }
        [JsonProperty("refX", NullValueHandling = NullValueHandling.Ignore)]
        public int? RefX { get; set; }
        [JsonProperty("refY", NullValueHandling = NullValueHandling.Ignore)]
        public int? refY { get; set; }
        [JsonProperty("style", NullValueHandling = NullValueHandling.Ignore)]
        public Dictionary<string, object> Style { get; set; }
        [JsonProperty("text", NullValueHandling = NullValueHandling.Ignore)]
        public string Text { get; set; }
        [JsonProperty("text-anchor", NullValueHandling = NullValueHandling.Ignore)]
        public string TextAnchor { get; set; }
    }
    public class Label
    {
        [JsonProperty("position")]
        public string Position { get; set; }
    }

    public class PortAttribute
    {
        [JsonProperty("out-circle", NullValueHandling = NullValueHandling.Ignore)]
        public OutCircle OutCircle { get; set; }
        [JsonProperty("circle", NullValueHandling = NullValueHandling.Ignore)]
        public Circle Circle { get; set; }

        [JsonProperty("text", NullValueHandling = NullValueHandling.Ignore)]
        public Dictionary<string,object> Text { get; set; }

        // 其他属性根据实际需要添加
        [JsonProperty("text-plus", NullValueHandling = NullValueHandling.Ignore)]
        public TextPlus TextPlus { get; set; }
    }

    public class Markup
    {
        [JsonProperty("tagName")]
        public string TagName { get; set; }

        [JsonProperty("selector")]
        public string Selector { get; set; }
    }

    public class PortItem
    {
        [JsonProperty("group")]
        public string Group { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("attrs",NullValueHandling = NullValueHandling.Ignore)]
        public PortAttribute Attrs { get; set; }
    }

    // 变量类
    public class UserVariable
    {
        private string _variableValue;
        [JsonProperty("variableId")]
        public string ID { get; set; }

        [JsonProperty("nodeId")]
        public string NodeID { get; set; }

        [JsonProperty("codeParamName")]
        public string CodeParamName { get; set; }

        [JsonProperty("variable")]
        public string VariableName { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("value")]
        public object Value { get; set; }
        [JsonProperty("label")]
        public string Description { get; set; }
        /// <summary>
        /// 标识是常量还是变量
        /// </summary>
        [JsonProperty("valueType")]
        public string ValueType { get; set; }

        [JsonProperty("variableNodeId")]
        public string VariableNodeID { get; set; }
        [JsonProperty("variableValue")]
        public string VariableValue
        {
            get => _variableValue; set
            {
                // 首次反序列化时，OriginalVariableValue 为 null，将其备份
                if (OriginalVariableValue == null)
                {
                    OriginalVariableValue = value;
                }
                _variableValue = value;
            }
        }
        [JsonProperty("reference")]
        public string Reference { get; set; }

        [JsonProperty("variableType")]
        public string VariableType { get; set; }


        [JsonProperty("constantValue")]
        public string ConstantValue { get; set; }

        [JsonProperty("options")]
        public List<string> Options { get; set; }

        //[JsonProperty("sourceNode")]
        //public string SourceNode { get; set; }
        [JsonIgnore]
        public string OriginalVariableValue { get; set; }

        /// <summary>
        /// 变量所属activity的ID
        /// </summary>
        public string ActivityID { get; set; }

        public string CompositeVarName { get; set; }
    }

    
}